package com.kazm.toolkit.files;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import com.kazm.toolkit.Tool;

public class TxtFiles {
	private BufferedReader br;

	public List<String> readLines(String path) throws IOException {
		List<String> list = new LinkedList<String>();

		br = new BufferedReader(new FileReader(path));

		String line = br.readLine();

		while (line != null) {
			list.add(line);
			line = br.readLine();
		}

		return list;
	}

	public String readString(String path) throws IOException {
		String txt;

		br = new BufferedReader(new FileReader(path));

		StringBuilder sb = new StringBuilder();
		String line = br.readLine();

		while (line != null) {
			sb.append(line);
			sb.append(System.lineSeparator());
			line = br.readLine();
		}
		txt = sb.toString();
		return txt;
	}

	public void writeLines(List<String> list, String path)
			throws IOException {
		PrintWriter writer = new PrintWriter(path, "UTF-8");
		for (String str : list) {
			writer.println(str);
		}
		writer.close();
	}
	
	public void addPrefix(String prefix, String path) throws IOException{
		List<String> li = readLines(path);
		li = Tool.com.addPrefix(li, prefix);
		writeLines(li, path);
	}
	
	public void addSuffix(String prefix, String path) throws IOException{
		List<String> li = readLines(path);
		li = Tool.com.addSuffix(li, prefix);
		writeLines(li, path);
	}
	
	public void delSubstrin(String sub, String path) throws IOException{
		List<String> li = readLines(path);
		li = Tool.com.deleteSubstring(li, sub);
		writeLines(li, path);
	}


}
